<?php
/**
* 
*/
class CourseReportModel extends MY_Model
{
	
	public function __construct()
	{
		parent::__construct();
		$this->table = 'nmp_course';
	}


	//
	public function course_count()
	{
		//总课程数
		$query = $this->db->query("SELECT count(*) as course_num FROM ".$this->table);
		$res = $query->row_array();
		$data['course_num'] = $res['course_num'];

		//本月课程数
		$start = date('Y-m-01 00:00:00');
		$end = date('Y-m-d H:i:s');
		$query = $this->db->query("SELECT count(*) as month_num FROM ".$this->table." where addtime >= unix_timestamp('$start') AND addtime <= unix_timestamp('$end')");
		$res = $query->row_array();
		$data['month_num'] = $res['month_num'];

		//总预约次数
		$query = $this->db->query("SELECT count(*) as app_num FROM nmp_user_appoint");
		$res = $query->row_array();
		$data['app_num'] = $res['app_num'];

		return $data;
	}


	//折线图数据
	public function course_appoint_data($dtnum = 90){
		$date_arr = array();
		$appoint_arr = array();
		
		if (!is_numeric($dtnum)) {
			//处理日期
			$d1 = substr($dtnum,0,10);
			$d2 = substr($dtnum,-10);
			$dlist = $this->getDateFromRange($d1,$d2);
			
			for ($i=0; $i < count($dlist); $i++) 
			{
				$date_arr[] = substr($dlist[$i],-5);

				$start = date('Y-m-d 00:00:00',strtotime($dlist[$i]));
				$end = date('Y-m-d H:i:s',strtotime('+1 day',strtotime($dlist[$i]))-1);
				
				
				$query = $this->db->query("SELECT COUNT(*) as appoint_num FROM nmp_user_appoint WHERE addtime >= unix_timestamp('$start') AND addtime <= unix_timestamp('$end')");
				$res = $query->row_array();

				$appoint_arr[] = $res['appoint_num'] ? $res['appoint_num'] : 0;
			}
		}
		else
		{
			for ($i=$dtnum; $i > 0; $i--) 
			{
				$date_arr[] = date('m-d',strtotime('-'.$i.' day'));

				$start = date('Y-m-d 00:00:00',strtotime('-'.$i.' day'));
				$end = date('Y-m-d 00:00:00',strtotime('-'.($i-1).' day'));

				$query = $this->db->query("SELECT COUNT(*) as appoint_num FROM nmp_user_appoint WHERE addtime >= unix_timestamp('$start') AND addtime <= unix_timestamp('$end')");
				$res = $query->row_array();

				$appoint_arr[] = $res['appoint_num'] ? $res['appoint_num'] : 0;
			}

		}

		$data['date_list'] = $date_arr;
		$data['appoint_num'] = $appoint_arr;

		return $data;

	}


	/**
	 * 获取指定日期段内每一天的日期
	 * @param  Date  $startdate 开始日期
	 * @param  Date  $enddate   结束日期
	 * @return Array
	 */
	function getDateFromRange($startdate, $enddate){
		
	    // 计算日期段内有多少天
	    $days = $this->diff_date($startdate,$enddate)+1;

	    // 保存每天日期
	    $date = array();
	    for($i=0; $i<$days; $i++){
	        $date[] = date('Y-m-d', strtotime(" +{$i} day",strtotime($startdate)));
	    }

	    return $date;
	}

	function diff_date($date1, $date2){
		if($date1>$date2){
			$startTime = strtotime($date1);
		$endTime = strtotime($date2);
		}else{
		$startTime = strtotime($date2);
		$endTime = strtotime($date1);
		}
		$diff = $startTime-$endTime;
		$day = $diff/86400;
		return intval($day);
	}

	//
	public function get_appoint($vid,$cid,$sdate,$k,$page=1,$size=50){
		
		if (!empty($sdate)) {//处理日期
			$d1 = substr($sdate,0,10);
			$d2 = substr($sdate,-10);
			$start = strtotime($d1);
			$end = strtotime($d2)+86400;
		}
		else{
			$start = strtotime(date('Y-m-01 00:00:00'));
			$end = strtotime(date('Y-m-d H:i:s'));
		}
		
		
		$where = "ap.addtime >= '{$start}' AND ap.addtime < '{$end}'";

		if (intval($vid) > 0) {
			$where .= " AND ap.shop_id =".$vid;
		}

		if (intval($cid) > 0) {
			$where .= " AND ap.mid =".$cid;
		}

		if (!empty($k)){
			$where .= " AND (c.name LIKE '%".$k."%')";
		}
		$sql = "SELECT ap.addtime, ap.sdate, ap.stime, ap.num, co.nickname, c.name as course_name, ca.card_name, ca.price FROM nmp_user_appoint as ap 
			LEFT JOIN nmp_coach as co ON ap.mid = co.id 
			JOIN nmp_course as c ON ap.cid = c.id 
			JOIN nmp_user_card as uc ON ap.card_id = uc.id 
			JOIN nmp_course_card as ca ON uc.card_id = ca.id 
			WHERE ".$where." ORDER BY ap.addtime DESC LIMIT ".($page-1)*$size.",".$size;

		$query = $this->db->query($sql);
		$data = $query->result_array();

		foreach ($data as $key => $value) {
			$data[$key]['addtime'] = date('Y-m-d', $value['addtime']);
		}

		$query = $this->db->query("select COUNT(*)  as num from nmp_user_appoint as ap 
			LEFT JOIN nmp_coach as co ON ap.mid = co.id 
			JOIN nmp_course as c ON ap.cid = c.id 
			JOIN nmp_user_card as uc ON ap.card_id = uc.id 
			JOIN nmp_course_card as ca ON uc.card_id = ca.id 
			WHERE ".$where);
		$row = $query->row_array();

		//总数
		$count = $row['num'];
		//总页数
		$pages = ceil($count/$size);
		
		return array('list'=>$data,'count'=>$count, 'pages'=>$pages);

	}


}




?>